﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace loader_update
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.BackColor = Color.FromArgb(45, 45, 48);    // màu đen
            lbl_title.Text = "Update installing ...";
        }

        private string _path = Application.StartupPath + @"\";
        private string _app_new = "BackupRestoreSoftwares.exe";
        private string _app_old = "BackupRestoreSoftwares.exe";
        private string _app_rn = "BackupRestoreSoftwares.bak";
        private string _app_dl = "update_app.exe";
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(2000);
            if (File.Exists(_path + _app_dl))
            {
                File.Move(_app_old, _app_rn);   // change name old
                File.Move(_app_dl, _app_new);   // change name down to new
                Thread.Sleep(1000);
                File.Delete(_app_dl);
                File.Delete(_app_rn);
            }
            lbl_title.Invoke((Action)(() => lbl_title.Text = "Updated successfull !"));
            Thread.Sleep(2000);
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (File.Exists(_path + @"BackupRestoreSoftwares.exe"))
            {
                Process.Start("BackupRestoreSoftwares.exe");
                Process.GetCurrentProcess().Kill();
            }
            else
            {
                Application.Exit();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }
    }
}
